import Vue from "vue";
import Vuex from "vuex";
import Bluetooth from "@/store/bluetooth/index.js";
import purchasing from "@/store/purchasing/index.js";
import accountManagement from "@/store/accountManagement/index.js";
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		sysinfo: uni.getSystemInfoSync(), //系统信息
		// 商户id
		mid: "",
		//  出货
		ShipmentList: [],
		// 退货
		returnList: [],
		//  出货备份
		ShipmentListCopy: [],
		// 退货备份
		returnListCopy: [],
		newShipmentList: [],
		newreturnList: [],
		// 版本号
		appVersion: "",
		// numList1:[],
		// numList2:[],
	},
	mutations: {
		// 出货增加
		addProduct(state, payload) {
			// console.log("addProduct");
			state.ShipmentList.push(payload);
			state.ShipmentListCopy = uni.$u.deepClone(state.ShipmentList);
			// console.log(state.ShipmentList);
			// console.log(state.ShipmentListCopy);
		},
		// 退货增加
		returnProduct(state, payload) {
			// console.log("returnProduct");
			state.returnList.push(payload);
			state.returnListCopy = uni.$u.deepClone(state.returnList);
			// console.log(state.returnListCopy);
		},
		// 合并数据
		MergeData(state, payload) {
			// 出货
			if (state.ShipmentList.length > 0) {
				state.ShipmentList.forEach((item) => {
					item.orderNum = +item.orderNum;
					item.realPrice = +item.realPrice;
					var p = state.newShipmentList.find((p) => p.proId == item.proId&&p.realPrice==item.realPrice);
					if (p) {
						// console.log(p.realPrice!=item.realPrice)
						p.orderNum += item.orderNum;
						
					} else {
						state.newShipmentList.push(item);
					}
				});
			}
			state.ShipmentList = state.newShipmentList;
			state.newShipmentList = []
			// 退货
			if (state.returnList.length > 0) {
				state.returnList.forEach((item) => {
					item.orderNum = +item.orderNum;
					item.realPrice = +item.realPrice;
					var p = state.newreturnList.find((p) => p.proId == item.proId&&p.realPrice==item.realPrice);
					if (p) {
						p.orderNum += item.orderNum;
					} else {
						state.newreturnList.push(item);
					}
				});
			}
			state.returnList = state.newreturnList;
			state.newreturnList = []
			// console.log('1', state.ShipmentListCopy);

		},
		// 展开数据
		ExpandData(state, payload) {
			state.ShipmentList = state.ShipmentListCopy;
			state.returnList = state.returnListCopy;
			this.newShipmentList = []
			this.newreturnList = []
			state.ShipmentListCopy = uni.$u.deepClone(state.ShipmentListCopy);
			state.returnListCopy = uni.$u.deepClone(state.returnListCopy);



		},
		// 清楚仓库
		clear(state, payload) {
			state.ShipmentList = []
			state.returnList = []
			state.ShipmentListCopy = []
			state.returnListCopy = []
			state.newShipmentList = []
			state.newreturnList = []
		},
		// 复制订单
		copyOrder(state, payload) {
			state.ShipmentList = payload.ShipmentList.map(item => {
				return {
					...item,
					disabled: true
				}
			})
			state.returnList = payload.returnList.map(item => {
				return {
					...item,
					disabled: true
				}
			})
			state.ShipmentListCopy = uni.$u.deepClone(state.ShipmentList);
			state.returnListCopy = uni.$u.deepClone(state.returnList);

		},
		// 存商户id
		changeMid(state, payload) {
			state.mid = payload;
		},
		// 存版本
		// saveAppVersion(state, payload) {
		// 	state.appVersion = payload
		// },
		//核算数据
		accounts1(state, payload) {
			state.ShipmentList = (payload);
			state.ShipmentListCopy = uni.$u.deepClone(state.ShipmentList);
		},
		accounts2(state, payload) {
				state.returnList = (payload);
				state.returnListCopy = uni.$u.deepClone(state.returnList);
		},
		// 修改单个产品
		editItem1(state, payload){
			// state.ShipmentList[payload.index]=payload.item
			state.ShipmentList.splice(payload.index,1,payload.item)
			state.ShipmentListCopy = uni.$u.deepClone(state.ShipmentList);
			uni.setStorageSync("ShipmentList",state.ShipmentList)
			// console.log(state.ShipmentListCopy)
			// console.log(state.ShipmentList)
		},
		editItem2(state, payload){
			state.returnList.splice(payload.index,1,payload.item)
			state.returnListCopy = uni.$u.deepClone(state.returnList);
			uni.setStorageSync("returnList",state.returnList)
		},
		// 删除单个产品
		delItem1(state, payload){
			// splice 
			let data = state.ShipmentList.splice(payload.index,1)
			state.ShipmentListCopy = uni.$u.deepClone(state.ShipmentList);
		},
		delItem2(state, payload){
			state.returnList.splice(payload.index,1)
			state.returnListCopy = uni.$u.deepClone(state.returnList);
		},
	},
	actions: {},
	modules: {
		Bluetooth,
		purchasing,
		accountManagement
	},
});
export default store;
